package edu.lnu.ums.entities.processes;

import edu.lnu.ums.entities.core.AbstractProcess;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class Amortization extends AbstractProcess {
    public static final int HUNDRED_PERCENTS = 100;

    @Column
    @Temporal(TemporalType.DATE)
    private Date startUsing;

    @Column
    @Temporal(TemporalType.DATE)
    private Date endUsing;

    @Column
    @Temporal(TemporalType.DATE)
    private Date useByDate;

    public Date getEndUsing() {
        return endUsing;
    }

    public void setEndUsing(Date endUsing) {
        this.endUsing = endUsing;
    }

    public Date getStartUsing() {
        return startUsing;
    }

    public void setStartUsing(Date startUsing) {
        this.startUsing = startUsing;
    }

    public Date getUseByDate() {
        return useByDate;
    }

    public void setUseByDate(Date useByDate) {
        this.useByDate = useByDate;
    }
    
    // in percents
    public int getWorning() {
        if (getUseByDate() == null || getStartUsing() == null) {
            return 0;
        }

        Date realUseEnd = getEndUsing() == null ? new Date() : getEndUsing();

        long warrantyInterval = getUseByDate().getTime() - getStartUsing().getTime();
        long realUsageInterval = realUseEnd.getTime() - getStartUsing().getTime();

        if (warrantyInterval == 0) {
            return 0;
        }

        return (int) (realUsageInterval * HUNDRED_PERCENTS / warrantyInterval);
    }
}
